package offertest.test02;

import java.util.Random;

/**
 * @ClassName M2
 * @Description
 * @Author ZJX
 * @Date 2025/4/7 20:47
 * @Version 1.0
 **/
public class M2 extends Thread{
    static int sum = 100;
    static int count = 3;
    static int[] result = new int[3];

    @Override
    public void run() {
        synchronized (M2.class){
            if (count > 0){
                Random random = new Random();
                int curCount ;
                if (count == 1){
                    curCount = sum;
                } else {
                    curCount = random.nextInt(sum / count)+1;
                }

//                将获得的钱放到结果集
                result[3-count] = curCount;
                sum -= curCount;
                count--;
                System.out.println(Thread.currentThread().getName()+"获得了红包:" + curCount);
            }
        }
    }
}
